package com.songqintuan.platform.tree;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.songqintuan.common.constant.GlobalConstants;
import com.songqintuan.common.util.SpringFacesUtil;
import com.songqintuan.model.po.AuthorityResources;
import com.songqintuan.model.po.RoleResource;
import com.songqintuan.persist.service.hibernate.IAuthorityResourcesService;
import com.songqintuan.persist.service.hibernate.IRoleResourceService;
import com.songqintuan.platform.common.constant.PlatformSpringBeanConstants;

public class ResourceDTree {

	private Map<String, Long> pids = new HashMap<String, Long>();

	private String treeResource;
	public Map<String, Long> getPids() {
		return pids;
	}

	public void setPids(Map<String, Long> pids) {
		this.pids = pids;
	}

	public String getTreeResource() {
		return treeResource;
	}

	public void setTreeResource(String treeResource) {
		this.treeResource = treeResource;
	}

	public ResourceDTree() {

	}
	//普通树
	public void getResource() {
		String treeStr;
		StringBuffer result = new StringBuffer();
		IAuthorityResourcesService<AuthorityResources, Long> authorityResourcesService = (IAuthorityResourcesService<AuthorityResources, Long>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.AUTHORITY_RESOURCES_SERVICE_BEAN_NAME);
		authorityResourcesService.setEntityClass(AuthorityResources.class);
		authorityResourcesService.setPKClass(Long.class);
		List<AuthorityResources> resources = authorityResourcesService.getAll(
				"sort", GlobalConstants.ORDER_BY_ASC_TRUE);
		treeStr = createTreeInfo(resources);
		this.setTreeResource(treeStr);
	}
	
	/** */
	/**
	 * 　　　　 * @生成创建tree的脚本 　　　　 * @param treeInfoList 　　　　 * @return 　　　　
	 */

	private String createTreeInfo(List<AuthorityResources> treeInfoList){
		StringBuffer contents = new StringBuffer();
		contents.append("<script type=\"text/JavaScript\">\n");
		contents.append("var d = new Dtree(\"d\");\n");
		contents.append("d.add(new Node(0,-1,\"资源管理\", \"javascript:go('')\",\"\",\"\",\"\",\"\",\"\"));\n");

		AuthorityResources treeInfo = null;
		for (int i = 0; i < treeInfoList.size(); i++) {
			treeInfo = (AuthorityResources) treeInfoList.get(i);
			contents.append(createOneItem(treeInfo));
		}
		contents.append("document.write(d);\n");
		contents.append("</script>");
		return contents.toString();
	}
	private String createOneItem(AuthorityResources treeInfo) {
		StringBuffer result = new StringBuffer();
		result.append("d.add(new Node(");
		//第一个参数表示当前节点的ID
		result.append(treeInfo.getId() + ",");
		//第二个参数表示当前节点的父节点的ID,根节点的值为 -1
		result.append(treeInfo.getPid() + ",");
		//第三个参数节点要显示的文字
		result.append("\"" + treeInfo.getLabel() + "\",");
		//第四个参数节点的Url
		result.append("\"" + "javascript:executeClickEvent('" + treeInfo.getId() + "')" + "\",");
		//第五个参数鼠标移至该节点时节点的Title
		result.append("\"" + treeInfo.getLabel() + "\",");
		//第六个参数节点的target
		result.append("\"\",");
		//第七个参数用做节点的图标,节点没有指定图标时使用默认值
		result.append("\"\",");
		//第八个参数用做节点打开的图标,节点没有指定图标时使用默认值
		result.append("\"\",");
		//第九个参数判断节点是否打开
		result.append(false);
		result.append("));\n");
		return result.toString();
	}
	//当前角色对应的带checkbox的树
	public void getCheckBoxResource(String roleId) {
		String treeStr;
		StringBuffer result = new StringBuffer();
		IAuthorityResourcesService<AuthorityResources, Long> authorityResourcesService = (IAuthorityResourcesService<AuthorityResources, Long>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.AUTHORITY_RESOURCES_SERVICE_BEAN_NAME);
		authorityResourcesService.setEntityClass(AuthorityResources.class);
		authorityResourcesService.setPKClass(Long.class);
		List<AuthorityResources> resources = authorityResourcesService.getAll(
				"sort", GlobalConstants.ORDER_BY_ASC_TRUE);
		IRoleResourceService<RoleResource, String> roleResourceService = (IRoleResourceService<RoleResource, String>) SpringFacesUtil
				.getSpringBean(PlatformSpringBeanConstants.ROLE_RESOURCE_SERVICE_BEAN_NAME);
		roleResourceService.setEntityClass(RoleResource.class);
		roleResourceService.setPKClass(String.class);
		List<RoleResource> roleResources = roleResourceService.findBy("roleId", roleId);
		treeStr = createCheckBoxTreeInfo(resources,roleResources);
		this.setTreeResource(treeStr);
	}
	
	/** */
	/**
	 * 　　　　 * @生成创建tree的脚本 　　　　 * @param treeInfoList 　　　　 * @return 　　　　
	 */

	private String createCheckBoxTreeInfo(List<AuthorityResources> treeInfoList, List<RoleResource> roleResources){
		StringBuffer contents = new StringBuffer();
		contents.append("<script type=\"text/JavaScript\">\n");
		contents.append("var d = new Dtree(\"d\");\n");
		contents.append("d.config.showCheckbox = true;\n");
		contents.append("d.add(new CheckboxNode(0,-1,\"资源管理\", false,\"\",\"\",\"\",\"\",\"\",false,false,false));\n");
		AuthorityResources treeInfo = null;
		Map<Long,RoleResource> roleResourceMap = new HashMap<Long,RoleResource>();
		if (null != roleResources && !(roleResources.isEmpty())) {
			Iterator<RoleResource> it = roleResources.iterator();
			RoleResource roleResource = null;
			while (it.hasNext()) {
				roleResource = it.next();
				roleResourceMap.put(roleResource.getResourceId(), roleResource);
			}
		}
		if (null != treeInfoList && !(treeInfoList.isEmpty())) {
			boolean checked = false;
			for (int i = 0; i < treeInfoList.size(); i++) {
				treeInfo = (AuthorityResources) treeInfoList.get(i);
				if (null != roleResourceMap.get(treeInfo.getId())) {
					checked = true;
				} else {
					checked = false;
				}
				contents.append(createCheckBoxOneItem(treeInfo,checked));
			}
		}
		contents.append("document.write(d);\n");
		contents.append("d.oAll(true)");
		contents.append("</script>");
		return contents.toString();
	}
	private String createCheckBoxOneItem(AuthorityResources treeInfo,boolean checked) {
		StringBuffer result = new StringBuffer();
		result.append("d.add(new CheckboxNode(");
		//第一个参数表示当前节点的ID
		result.append(treeInfo.getId() + ",");
		//第二个参数表示当前节点的父节点的ID,根节点的值为 -1
		result.append(treeInfo.getPid() + ",");
		//第三个参数节点要显示的文字
		result.append("\"" + treeInfo.getLabel() + "\",");
		//第四个参数节点的Url
		result.append("false,");
		//第五个参数鼠标移至该节点时节点的Title
		result.append("\"\",");
		//第六个参数节点的target
		result.append("\"\",");
		//第七个参数用做节点的图标,节点没有指定图标时使用默认值
		result.append("\"\",");
		//第八个参数用做节点打开的图标,节点没有指定图标时使用默认值
		result.append("\"\",");
		//第九个参数判断节点是否打开
		result.append("\"\",");
		result.append("true,");
		result.append(checked+",");
		result.append("false,");
		result.append("\"resource\"");
		result.append("));\n");
		return result.toString();
	}
	
	
}
